part of 'widget.dart';

class _AppDropdownInputView<T> extends StatefulWidget {
  const _AppDropdownInputView({
    required this.value,
    required this.onChanged,
    required this.items,
    this.width,
  });

  final T? value;
  final ValueChanged<T> onChanged;
  final List<AppDropdownInputItem<T>> items;
  final double? width;

  @override
  State<_AppDropdownInputView<T>> createState() => _AppDropdownInputViewState<T>();
}

class _AppDropdownInputViewState<T> extends State<_AppDropdownInputView<T>> {
  bool _isOpen = false;

  Widget _buildItemTrailing({
    required bool selected,
    required BuildContext context,
  }) {
    const kTrailingWidth = 40.0;

    if (selected) {
      return SizedBox(
        width: kTrailingWidth,
        child: Center(
          child: Icon(
            Icons.check,
            size: 18,
            color: CoreTheme.of(context).primaryColor,
          ),
        ),
      );
    }

    return const SizedBox(
      width: kTrailingWidth,
    );
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.width,
      child: DropdownButtonHideUnderline(
        child: DropdownButton2<T>(
          onChanged: (value) {
            widget.onChanged(value as T);
          },
          style: _AppDropdownInputTheme.of(context).textTheme.item,
          isExpanded: true,
          onMenuStateChange: (value) {
            setState(() {
              _isOpen = value;
            });
          },
          selectedItemBuilder: (context) {
            return widget.items.map((item) {
              return Align(
                alignment: Alignment.centerLeft,
                child: Padding(
                  padding: const EdgeInsets.only(
                    left: 13,
                  ),
                  child: Text(
                    item.label,
                    style: _AppDropdownInputTheme.of(context).textTheme.item,
                  ),
                ),
              );
            }).toList();
          },
          value: widget.value,
          iconStyleData: IconStyleData(
            iconSize: 18,
            icon: SizedBox(
              width: 40,
              child: Center(
                child: Icon(
                  CupertinoIcons.chevron_down,
                  color: _AppDropdownInputTheme.of(context).iconColor,
                ),
              ),
            ),
            openMenuIcon: SizedBox(
              width: 40,
              child: Center(
                child: Icon(
                  CupertinoIcons.chevron_up,
                  color: _AppDropdownInputTheme.of(context).iconColor,
                ),
              ),
            ),
          ),
          enableFeedback: false,
          buttonStyleData: ButtonStyleData(
            decoration: BoxDecoration(
              borderRadius: _isOpen
                  ? const BorderRadius.vertical(
                      top: Radius.circular(10),
                    )
                  : BorderRadius.circular(10),
              color:  _AppDropdownInputTheme.of(context).itemBackgroundColor,
              border: Border.all(
                width: 0.7,
                color: _AppDropdownInputTheme.of(context).borderColor,
                strokeAlign: BorderSide.strokeAlignOutside,
              ),
            ),
            padding: EdgeInsets.zero,
          ),
          dropdownStyleData: DropdownStyleData(
            decoration: BoxDecoration(
              color: _AppDropdownInputTheme.of(context).itemBackgroundColor,
              borderRadius: const BorderRadius.vertical(
                bottom: Radius.circular(10),
              ),
              border: Border.all(
                width: 0.7,
                color: _AppDropdownInputTheme.of(context).borderColor,
                strokeAlign: BorderSide.strokeAlignOutside,
              ),
            ),
            padding: EdgeInsets.zero,
            openInterval: const Interval(0.0, 0.0),
          ),
          menuItemStyleData: MenuItemStyleData(
            height: 40,
            padding: EdgeInsets.zero,
            overlayColor: MaterialStateProperty.resolveWith((states) {
              return states.contains(MaterialState.hovered)
                  ?  _AppDropdownInputTheme.of(context).hoveredItemBackgroundColor
                  : Colors.transparent;
            }),
          ),
          items: widget.items.map((item) {
            return DropdownMenuItem<T>(
              value: item.value,
              child: Row(
                children: [
                  const SizedBox(
                    width: 13,
                  ),
                  Expanded(
                    child: Text(
                      item.label,
                      style: _AppDropdownInputTheme.of(context).textTheme.item,
                    ),
                  ),
                  _buildItemTrailing(
                    selected: widget.value == item.value,
                    context: context,
                  ),
                ],
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}
